#include "clkApplication.h"

clkApplication::clkApplication()
{
	m_appName = "clkApplication";
	m_appHwnd = 0;
	m_appHinstance = 0;
	m_appProperties = 0;
}

clkApplication::~clkApplication()
{
	shutApplication();
}

LRESULT CALLBACK wProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message){case WM_DESTROY:{PostQuitMessage(0);return 0;}break;}
	return DefWindowProc(hWnd,message,wParam,lParam);
}

void clkApplication::initApplication(
	char* a_name,
	V2DF  a_coordinate,
	V2DF  a_dimension)
{
	//define window properies
	m_appName = a_name;
	m_appProperties  = (WS_OVERLAPPEDWINDOW) & ~(WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_THICKFRAME);
	m_appCoordinate = a_coordinate;
	m_appRDimension = a_dimension;

	//disable console window buttons
	SetConsoleTitle(m_appName.data());
	DWORD sty = GetWindowLong(GetConsoleWindow(),GWL_STYLE);
	sty &= ~(WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_SYSMENU);
	SetWindowLong(GetConsoleWindow(),GWL_STYLE,sty);
	SetWindowPos(GetConsoleWindow(),0,0,0,0,0,
		SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOOWNERZORDER);

	//define window class properies
	WNDCLASSEX wclass;
	ZeroMemory(&wclass,sizeof(WNDCLASSEX));
	wclass.cbSize        = sizeof(WNDCLASSEX);
	wclass.lpfnWndProc   = (WNDPROC)wProc;
	wclass.hInstance     = GetModuleHandle(0);
	wclass.hCursor       = LoadCursor(0,IDC_ARROW);
	wclass.lpszClassName = TEXT("WindowClass");

	//create window
	RegisterClassEx(&wclass);
	m_appHinstance = GetModuleHandle(0);
	m_appHwnd = CreateWindowEx(
		0,wclass.lpszClassName,
		m_appName.data(),
		m_appProperties,
		(int)m_appCoordinate.x,
		(int)m_appCoordinate.y,
		(int)m_appRDimension.x,
		(int)m_appRDimension.y,0,0,
		m_appHinstance,0);

	//get client window size
	RECT rect; GetClientRect(m_appHwnd,&rect);
	m_appCDimension = V2DF((float)rect.right,(float)rect.bottom);

	//display window
	ShowWindow(m_appHwnd,SW_SHOWNORMAL);
	UpdateWindow(m_appHwnd);
}

void clkApplication::shutApplication()
{

}

bool clkApplication::exit()
{
	static MSG msg;
	if(PeekMessage(&msg,0,0,0,PM_REMOVE))
	{
		if(msg.message == WM_QUIT)
			return false;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return true;
}